﻿using System;
using System.Collections.Generic;
using System.Windows.Threading;
using Sanford.Multimedia.Midi;

namespace GentlyWeep
{
    //public delegate void GtEventHandler(object sender, WeepEventArgs e);
    class MidiPlayer
    {
        DispatcherTimer timergt = new DispatcherTimer();
        WeepEventArgs we = new WeepEventArgs();
        
        public event GtEventHandler midiOn;
        private int Position;
        String asd;

        public int Position1
        {
            get { return Position; }
            set { Position = value; }
        }

        private int posCon;
        

        public MidiPlayer()
        {
            posCon = 0;
            Position = 0;
            this.timergt.Tick += new System.EventHandler(this.timergt_Tick);
            this.InitializeLine();
        }

        public void Start()
        {
            timergt.Start();
        }

        public void stop()
        {
            timergt.Stop();
        }



        private void timergt_Tick(object sender, EventArgs e)
        {
            Position++;
            //Console.WriteLine(">" + Position);
            if (posCon < gtlist.Count)
            {
                
                if (gtlist[posCon][0] == Position)
                {
                    Console.WriteLine(gtlist[posCon][1] + " , " + gtlist[posCon][2] + " , " + gtlist[posCon][3] + " , ");
                    we.tick = gtlist[posCon][0];
                    we.Data1 = gtlist[posCon][1];
                    we.MidiChannel = 9;
                    midiOn(this,we);
                    posCon++;
                }
            }
        }

        List<int[]> gtlist = new List<int[]>();

        //testc Midi
        private void InitializeLine()
        {//10000 = 1 milliseconds
            gtlist.Add(new int[] {10000, 69, 4, 0 });
            gtlist.Add(new int[] {20000, 71, 4, 0 });
            gtlist.Add(new int[] {30000, 62, 4, 0 });
            gtlist.Add(new int[] {40000, 64, 4, 0 });
            //gtlist.Sort();
        }


    }
    public class WeepEventArgs : EventArgs
    {
        public ChannelCommand Command;
        public int MidiChannel;
        public int Data1;
        public int tick;
    }
}
